package org.bw.bookstore.book.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.io.IOUtils;
import org.bw.bookstore.book.config.OssUtile;
import org.bw.bookstore.book.mapper.MemberMapper;
import org.bw.bookstore.book.utils.ZXingCode;
import org.bw.bookstore.common.entity.TbUserInfo;
import org.bw.bookstore.common.utils.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;

/**
 * @program:server-store
 * @description:二维码
 * @author:guoshuai
 * @create:2020-10-29 19:50
 **/
@Service

public class UserQrcodeService {
    @Autowired
    MemberMapper memberMapper;
    public void setQrCords(Integer userId) {
         String randomString = StringTools.getRandomString(32);
         System.out.println(randomString);
         File logoFile = new File("D:\\images\\logo.jpg");
            File codeFile = new File("D:\\images\\sing_ewm.jpg");
            String url = randomString;
            Date date = new Date();
            String message="八维宏烨书店欢迎您";
            String note = message;
            ZXingCode.drawLogoQRCode(logoFile, codeFile, url, note);
            File file = new File("D:\\images\\sing_ewm.jpg");
         FileInputStream input = null;
         try {
             input = new FileInputStream(file);
         } catch (FileNotFoundException e) {
             e.printStackTrace();
         }

         MultipartFile multipartFile = null;
         try {
             multipartFile = new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));
         } catch (IOException e) {
             e.printStackTrace();
         }
         System.out.println(multipartFile);
        TbUserInfo tbUserInfo = new TbUserInfo();
        tbUserInfo.setId(userId);
        tbUserInfo.setMemberCode(randomString);
        int i = memberMapper.updateById(tbUserInfo);
        String s = OssUtile.UploadAliyun(multipartFile, multipartFile.getOriginalFilename());
         System.out.println(s);
     }

    public boolean getQrCords(Integer userId, String memberCode) {
        TbUserInfo tbUserInfo = new TbUserInfo();
        TbUserInfo user = memberMapper.selectOne(new QueryWrapper<TbUserInfo>()
                                                    .eq("id",userId)
                                                    .eq("member_code",memberCode)
        );
        if(user!=null){
            return true;
        }
        return false;
    }
}
